<template>
	<view>
		<cu-custom bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{task.name}}</block>
		</cu-custom>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">学号</view>
				<input placeholder="请输入学号" v-model="no" name="no"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">手机号码</view>
				<input placeholder="请输入手机号码" v-model="cellphone" name="cellphone"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">姓名</view>
				<input placeholder="请输入的姓名" v-model="username" name="username"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">二维码</view>
				<image :src="task.imgPath" mode="aspectFit"></image>
			</view>

			<view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/{{imgsize}}
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<imgsize">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-red margin-tb-sm lg" @tap="send()">嫣红</button>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				no:'',
				cellphone:'',
				username:'',
				task:{
					name:'行程码',
					imgPath:'/static/20201203123309.jpg'
				},
				imgsize:1,
				imgList: []
			};
		},
		methods: {
			send(){
				uni.uploadFile({
					url:'http://localhost:8088/task/add',
					formData:{
						stdNo:this.no,
						cellphone:this.cellphone,
						username:this.username
					},
					filePath:this.imgList[0],
					name:'file',
					fileType:'image',
					success:(e) =>{
						console.log(e)
					},
					fail: (e) => {
						console.log(e)
					},
					complete: () => {
					}
				})
			},
			DateChange(e) {
				this.date = e.detail.value
			},
			ChooseImage() {
				uni.chooseImage({
					count: this.imgsize, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: this.task.name,
					content: '确定要删除'+ this.task.name +'吗？',
					cancelText: '再想想',
					confirmText: '删除',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
